package com.orhon.smartcampus.modules.teacher.fetcher;

import java.util.ArrayList;
import java.util.List;

import com.orhon.smartcampus.auth.JwtUtils;
import com.orhon.smartcampus.modules.base.entity.Schools;
import com.orhon.smartcampus.modules.base.gqlModel.GQLSchool;
import com.orhon.smartcampus.modules.base.mm.SchoolMapper;
import com.orhon.smartcampus.modules.student.entity.Eclass;
import com.orhon.smartcampus.modules.student.entity.EclassHeadteacher;
import com.orhon.smartcampus.modules.student.gqlModel.GQLEclass;
import com.orhon.smartcampus.modules.student.gqlModel.GQLHeadTeacher;
import com.orhon.smartcampus.modules.student.mm.EclassHeadteacherMapper;
import com.orhon.smartcampus.modules.student.mm.EclassMapper;
import com.orhon.smartcampus.modules.teacher.entity.TInformation;
import com.orhon.smartcampus.modules.teacher.gqlModel.GQLTeacher;
import com.orhon.smartcampus.modules.teacher.mm.TeacherMapper;
import com.orhon.smartcampus.modules.user.dao.TokenService;
import com.orhon.smartcampus.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.orhon.smartcampus.basicComponents.Result;
import com.orhon.smartcampus.modules.base.gqlModel.GQLPageParam;
import com.orhon.smartcampus.modules.base.mm.EnvArgsMapper;
import com.orhon.smartcampus.modules.teacher.dao.UndertakeProjectDao;
import com.orhon.smartcampus.modules.teacher.entity.UndertakeProject;
import com.orhon.smartcampus.modules.teacher.gqlModel.GQLUndertakeProject;
import com.orhon.smartcampus.modules.teacher.mm.UndertakeProjectMapper;

import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;

@Component
public class UndertakeProjectsFetcher implements DataFetcher {
	
	@Autowired
	UndertakeProjectDao undertakeProjectDao;

	@Autowired
	TokenService tokenService;

	@Override
	public Object get(DataFetchingEnvironment env) throws Exception {

		String token = tokenService.getSystemToken();
		if(token==null) {
			return  R.NoLogin();
		}
		Long schoolId = JwtUtils.getSchoolId(token);

		List<GQLUndertakeProject> GQLUndertakeProject = new ArrayList<>();
		GQLPageParam page = EnvArgsMapper.MAPPER.fromArgs(env);
		String hql = "from UndertakeProject where deleted_at = null and school_id = " + schoolId;

		if(env.getArgument("teacher_id")!=null) {
			int id = Integer.parseInt(env.getArgument("teacher_id").toString());
			hql += " and teacher_id = " + id;
		}
		List<UndertakeProject> list = undertakeProjectDao.find(hql,null,page.getPage(),page.getLimit());
//		for (UndertakeProject item : list) {
//
//			GQLTeacher gqlTeacher = null;
//			if (env.getSelectionSet().contains("data/teacher")){
//				TInformation tInformation = item.getTeacherTInformationByTeacherId();
//				gqlTeacher = TeacherMapper.MAPPER.toTeacher(tInformation);
//			}
//
//			GQLSchool gqlSchool = null;
//			if (env.getSelectionSet().contains("data/school")){
//				Schools schools = item.getBaseSchoolsBySchoolId();
//				gqlSchool = SchoolMapper.MAPPER.toSchool(schools);
//			}
//
//			GQLUndertakeProject undertakeProject = UndertakeProjectMapper.MAPPER.mapEntity(item);
//			undertakeProject.setSchool(gqlSchool);
//			undertakeProject.setTeacher(gqlTeacher);
//			GQLUndertakeProject.add(undertakeProject);
//
//		}


		List<GQLUndertakeProject> mapEntityList = UndertakeProjectMapper.MAPPER.mapEntityList(list);

		Long total = 0L;
		if (env.getSelectionSet().contains("total")){
			total = undertakeProjectDao.getTotalCount(hql);
		}
		return Result.ok().put("data", mapEntityList).put("total", total);
	}
}
